<?php 

require_once("form/test.php");

class SelectForm extends Form {
	
	public function __construct(){
		parent::__construct(array(
			"gender" => new SelectField($this->genders()),
		));
	}
	
	private function genders(){
		return array(
			new Option("F", "Female"),
			new Option("M", "Male")
		);
	}
	
}

class SelectFieldTest extends Test {
	
	public function run(){
		$form = new SelectForm();
		$this->tags($form);
		$this->invalid($form);
		$this->valid($form);
	}
	
	private function tags($form){
		$this->assertEquals($form->render("gender"), "<select name=\"gender\" id=\"gender\"><option value=\"F\">Female</option><option value=\"M\">Male</option></select>");
		$form->populate(array("gender" => "M"));
		$this->assertEquals($form->render("gender"), "<select name=\"gender\" id=\"gender\"><option value=\"F\">Female</option><option value=\"M\" selected=\"selected\">Male</option></select>");
		$form->clean();
	}
	
	private function invalid($form){
		$this->assertEquals(FALSE, $form->isValid(), "Form is not valid");
		$this->assertTrue(in_array("gender", $form->errors), "Gender contains error");
		$this->assertTrue($form->containsError("gender"), "Gender contains error");
		$this->assertEmpty($form->getValue("gender"));
		$form->clean();
	}
	
	private function valid($form){
		$form->populate(array("gender" => "My gender is Verta"));
		$this->assertTrue($form->isValid(), "Form valid");
		$this->assertFalse(in_array("gender", $form->errors), "Gender does NOT contain error cause it's valid");
		$this->assertFalse($form->containsError("gender"), "Gender does NOT contain error cause it's valid");
		$this->assertNotEmpty($form->getValue("gender"));
		$this->assertEquals($form->getValue("gender"), "My gender is Verta");
		$form->clean();
	}

	
}

$test = new SelectFieldTest();
$test->run();